package toy.keli.edic.ui.home.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import kotlinx.coroutines.flow.MutableStateFlow
import toy.keli.edic.R
import toy.keli.edic.ui.main.extend.CurrentScreenKeyboardListener
import toy.keli.edic.ui.main.extend.MainRun

class AutoCompleteTextFieldEvent{
    companion object {
        var setSearchKey: (key: String) -> Unit = {}
        var selectSearchKey: () -> Unit = {}
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AutoCompleteTextField(
    modifier: Modifier = Modifier,
    text:String = "",
    suggestions: MutableStateFlow<List<String>>?=null,
    onSearch: (String) -> Unit ={},
    onClear: () -> Unit ={},
    onCancel: () -> Unit ={},
    onDoneActionClick: MutableState<TextFieldValue>.(t:String) -> Unit = {},
    onItemClick: Int.(t:String) -> Unit = {},
    autoFocus :Boolean = true,
    requester:FocusRequester = FocusRequester(),
    itemContent: @Composable (t:String) -> Unit = {}
) {
    val searchQuery = rememberSaveable(stateSaver = TextFieldValue.Saver) {
        mutableStateOf(
            TextFieldValue(
                annotatedString = AnnotatedString(text = text),
                selection = if (text.length > 0) TextRange(0, text.length) else TextRange.Zero
            )
        )
    }
    remember {
        AutoCompleteTextFieldEvent.setSearchKey = {
            searchQuery.value = TextFieldValue(it, selection = TextRange(0, it.length))
            onSearch(it)
        }
        AutoCompleteTextFieldEvent.selectSearchKey = {
            val text = searchQuery.value.text
            searchQuery.value = TextFieldValue(text, selection = TextRange(0, text.length))
        }
        null
    }
    var expandedState by remember { mutableStateOf(false) }
    val sug = if (suggestions == null) listOf<String>() else suggestions.collectAsState().value


    var haveFocus = remember { false }

    if (autoFocus) {
        MainRun(200) {
            requester.requestFocus()
        }
    }

    expandedState = sug.isNotEmpty()
    Box {
        CurrentScreenKeyboardListener(modifier = Modifier.fillMaxSize()) {
            onCancel()
        }

        Column {
            TextField(
                value = searchQuery.value,
                onValueChange = {
                    searchQuery.value = it
                    onSearch(it.text)
                },
                singleLine = true,
                placeholder = {
                    Text(
                        text = "",
                        style = MaterialTheme.typography.displaySmall,
                        color = Color.Gray
                    )
                },
                leadingIcon = {
                    Icon(
                        painter = painterResource(R.drawable.search), contentDescription = null
                    )
                },
                trailingIcon = {
                    if (searchQuery.value.text.isNotEmpty()) {
                        IconButton(onClick = {
                            searchQuery.value = TextFieldValue()
                            onClear()
                        }) {
                            Icon(
                                painter = painterResource(R.drawable.clear),
                                contentDescription = null
                            )
                        }
                    }
                },
                colors = TextFieldDefaults.textFieldColors(
                    focusedIndicatorColor = Color.Transparent,
//                backgroundColor = MaterialTheme.colorScheme.surface,
                    unfocusedIndicatorColor = Color.Transparent
                ),
                keyboardActions = KeyboardActions(onDone = {
                    onDoneActionClick.invoke(searchQuery, searchQuery.value.text)
                }),
                keyboardOptions = KeyboardOptions(
                    imeAction = ImeAction.Done,
                    keyboardType = KeyboardType.Text
                ),
                shape = RoundedCornerShape(8.dp),
                modifier = Modifier
                    .fillMaxWidth()
                    .zIndex(1f)
                    .focusable()
                    .focusRequester(requester)
                    .onFocusChanged {
                        if (it.isFocused) {
                            onSearch(text)
                            haveFocus = true
                        } else if (haveFocus) {
                            onCancel()
                            haveFocus = false
                        }
                    },
            )
            if (sug.isNotEmpty()) {
                Card(
                    modifier = Modifier
                        .padding(5.dp)
                        .alpha(0.8f)
                        .verticalScroll(rememberScrollState())
//                , colors = CardDefaults.cardColors(containerColor = Color(0x99aaaaaa), contentColor = Color.Black)
                ) {
                    sug.forEachIndexed { i, it ->
                        var word = it
                        var ch = ""
                        if (it.indexOf(" : ") != -1) {
                            val split2 = it.split(" : ")
                            word = split2[0]
                            ch = split2[1]
                        }
                        Row(
                            modifier = Modifier.clickable {
                                onItemClick.invoke(i,word)
                            }.fillMaxWidth()
                                .height(30.dp)
                                .padding(start = 10.dp, bottom = 10.dp)
                        ) {
                            Text(word, modifier = Modifier.weight(1f))
                            if (ch.length > 0) {
                                Text(ch, color = Color.Gray)
                            }
                        }

                    }
                }
//        ExposedDropdownMenuBox(
//            expanded = expandedState,
//            onExpandedChange = {
//                if (it) {
//                    requester.requestFocus()
//                } else {
//                    focusManager.clearFocus()
//                }
//                expandedState = it
//            },
//            modifier = modifier.fillMaxWidth()
//        ) {
//            ExposedDropdownMenu(
//                expanded = expandedState,
//                onDismissRequest = {expandedState=true  },
//                modifier = Modifier.background(
//                    MaterialTheme.colorScheme.surface.copy(alpha = 0.2f)
//                )
//            ) {
//                sug.forEach { label ->
//                    DropdownMenuItem(
//                        onClick = {
//                            onItemClick(label)
//                            searchQuery = TextFieldValue(
//                                label.toString(),
//                                selection = TextRange(label.toString().length)
//                            )
//                            expandedState = false
//                    },text={
//                            itemContent(label)
//                    }
//                    )
//                }
//            }
//        }
            }

            itemContent(searchQuery.value.text)
        }

    }

}